import 'package:get/get.dart';
import 'package:honeypotflutter/modules/deliver/api/deliver_api.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'package:honeypotflutter/publicBussine/express/bussine_express_logic.dart';

class DeliverExpressLogic extends GetxController {
  /// 快递信息
  ExpressLogic expressLogic = Get.find<ExpressLogic>();

  /// 选中ID
  List<int> listID = [];

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    listID = expressLogic.expressUserList.map((e) => (e.id ?? -1)).toList();
  }

  /*
  * 点击事件
  * */
  selectAction(int? id) async {
    if (id == null) {
      return;
    }
    if (listID.contains(id)) {
      listID.remove(id);
    } else {
      listID.add(id);
    }
    update([id.toString()]);
  }

  /*
  * 确认
  * */
  sureAction() async {
    if (listID.isEmpty) {
      HLTotal.total("请选择一个快递公司");
      return;
    }
    await DeliverApi.setUserCourierCompany(listID);
    try {
      HLTotal.progress(status: "新设置请求中...");
      await expressLogic.requestUserExpressList();
      Get.back();
    } catch (error) {
    } finally {
      HLTotal.dismiss();
    }
  }
}
